const path = require('path');
const seeyonBuild = require('./seeyonBuild.json');
// 此处引用了各个功能的webpack插件，来实现统一的功能配置。
const BuildPlugin = require('./config/plugin/buildPlugin.js');
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin');
const IconfontPlugin = require('@fiber/iconfont-webpack-plugin');
const I18nWebpackPlugin = require('@fiber/i18n-webpack-plugin');
//输出路径配置
const devServer = require('./config/devServer.js'); // 代理配置

const isProduction = process.env.NODE_ENV === 'production';
// 选人控件图标
const seletOrgIconConfig = require('./config/iconfont/index.js');
// 上下文
const ctxPath = seeyonBuild.pathResolve.ctxPath;
//构建版本号
const buildVersion = seeyonBuild.pathResolve.staticSuffix;
const resolve = (dir) => path.join(__dirname, dir);

let vueConfig = {
	lintOnSave: false,
	productionSourceMap: !isProduction,
	baseUrl: isProduction ? `${ctxPath}/common` : '',
	outputDir: `dist/${ctxPath}/common`,
	assetsDir: 'static',
	pages: {
		index: {
			entry: 'src/views/selectorg.js',
			template: 'public/index.html',
			filename: isProduction ? 'selectorg.html' : 'index.html'
		}
	},
	//增加需要转为ES5的package包，vue-cli 4.x适配IE浏览器(SCRIPT1002:语法错误)
	transpileDependencies: ['./src', 'vue-virtual-scroller'],
	configureWebpack: {
		plugins: [
			new BuildPlugin(),
			new I18nWebpackPlugin(),
			new IconfontPlugin({
				downloadFile: false,
				cacheDir: resolve('./src/assets/cache'),
				projects: [
					{
						name: 'cap',
						id: 385729,
						icons: seletOrgIconConfig.seletOrgIconFont
					}
				]
			})
		],
		output: {
			// js打包后，要保证文件名一致，并且不能有缓存问题
			filename: `selectorg/[name].js${buildVersion}`,
			chunkFilename: `selectorg/[name].js${buildVersion}`
		},
		externals: {
			vue: 'window.Vue',
			vuex: 'window.Vuex',
			axios: 'window.axios',
			fiber: 'window.Fiber'
		},
		resolve: {
			alias: {
				'@': path.resolve('src')
			}
		}
	},
	chainWebpack(config) {
		if (isProduction) {
			config.plugins['delete']('preload-index');
			config.plugin('extract-css').tap((args) => [
				{
					filename: `selectorg/[name].css${buildVersion}`,
					chunkFilename: `selectorg/[name].css${buildVersion}`,
					ignoreOrder: true
				}
			]);
		}
	},
	css: {
		loaderOptions: {
			sass: {
				data: `
			  @import "@/css/theme.scss";
			  @import "@/css/tool.scss";
			`
			}
		}
	},
	devServer: devServer
};
module.exports = vueConfig;
